package io.renren.modules.app.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName: FileUtil
 * Package: io.renren.modules.app.utils
 * Description:
 *
 */
@Component
public class FileUtil {
        public static  String imgsrc = "D:\\image\\";
        public static String uploadimg(MultipartFile file) throws IOException {
            String originalFilename = file.getOriginalFilename();
            //获取原文件的后缀名
            if (originalFilename != null) {
                String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
                String uuid = UUID.randomUUID().toString();
                // 使用UUID和文件扩展名来创建一个新的文件名
                String newFilename = uuid + fileExtension;
                file.transferTo(new File(FileUtil.imgsrc + newFilename));
                String url ="http://localhost:8080"+"/renren-fast/uploadimg/" +newFilename;
                return url;
            }
            return "上传失败";
        }
}
